
const http = require('http')
const url = require('url')
const { join, extname } = require('path')
const fs = require('fs')


let logarr;  
let goodslistArr=[];

http.createServer((req, res) => {
  // console.log(req.headers);
  // 得到pathname和get参数
  let { pathname, query: get } = url.parse(req.url, true)
  // 接口一般都是返回json,所以统一设置
  res.setHeader('content-type', 'application/json;charset=utf-8')
  if (pathname !== '/favicon.ico') {
    if ('/' == pathname) {
    
      fs.readFile('/logs.txt', 'utf-8', (err, data) => {
        logarr=data.split('\n').slice(0,-1);
        logarr.reduce((value,item,index)=>{
            goodslistArr.push({
              path:item.split("--")[0],
              title:item.split("--")[1],
              price:item.split("--")[2]
            })
        })
        console.log(goodslistArr);
        res.end(JSON.stringify(goodslistArr));
      })
    } else {
      res.end(JSON.stringify({ code: 1000, msg: '没有此接口', data: null }))
    }
  }
}).listen(3000, '0.0.0.0',()=>{
  console.log("服务器已启动")
})